package com.zycfc.zsf.boot.web.util;

import javax.servlet.http.*;
import com.zycfc.zsf.boot.util.text.*;
import java.util.*;
import java.math.*;
import org.slf4j.*;

public final class RequestHelper
{
    private static final Logger LOGGER;
    private static final String RETURNURL = "returnUrl";
    private static final ThreadLocal<HttpServletRequest> REQUEST;
    
    public static void setLocalRequest(final HttpServletRequest request) {
        RequestHelper.REQUEST.set(request);
    }
    
    public static HttpServletRequest getLocalRequest() {
        return RequestHelper.REQUEST.get();
    }
    
    private RequestHelper() {
    }
    
    public static String getString(final String key) {
        return getString(key, "", true);
    }
    
    public static String getString(final String key, final String defaultValue, final boolean b) {
        final String value = RequestHelper.REQUEST.get().getParameter(key);
        if (StringUtils.isEmpty(value)) {
            return defaultValue;
        }
        if (b) {
            return value.replace("'", "''").trim();
        }
        return value.trim();
    }
    
    public static Map<String, Object> getQueryMap() {
        final Map map = new HashMap();
        final Enumeration params = RequestHelper.REQUEST.get().getParameterNames();
        while (params.hasMoreElements()) {
            final String key = params.nextElement().toString();
            final String[] values = RequestHelper.REQUEST.get().getParameterValues(key);
            if (key.equals("sortField") || key.equals("orderSeq") || key.equals("sortColumns")) {
                final String val = values[0].trim();
                if (StringUtils.isNotEmpty(val)) {
                    map.put(key, values[0].trim());
                }
            }
            if (values.length > 0 && StringUtils.isNotEmpty(values[0])) {
                if (key.startsWith("Q_")) {
                    addParameter(key, values, map);
                }
                else if (values.length == 1) {
                    final String val = values[0].trim();
                    if (!StringUtils.isNotEmpty(val)) {
                        continue;
                    }
                    map.put(key, values[0].trim());
                }
                else {
                    map.put(key, values);
                }
            }
        }
        return (Map<String, Object>)map;
    }
    
    private static void addParameter(final String key, final String[] values, final Map<String, Object> map) {
        final String[] aryParaKey = key.split("_");
        if (aryParaKey.length < 3) {
            return;
        }
        final String paraName = key.substring(2, key.lastIndexOf("_"));
        final String type = key.substring(key.lastIndexOf("_") + 1);
        if (values.length == 1) {
            String value = values[0].trim();
            if (StringUtils.isNotEmpty(value)) {
                try {
                    if (value.indexOf("_") != -1) {
                        value = value.replaceAll("_", "|_");
                    }
                    final Object obj = getObjValue(type, value);
                    map.put(paraName, obj);
                }
                catch (Exception e) {
                    RequestHelper.LOGGER.debug(e.getMessage());
                }
            }
        }
        else {
            final Object[] aryObj = getObjValue(type, values);
            map.put(paraName, aryObj);
        }
    }
    
    private static Object[] getObjValue(final String type, final String[] value) {
        final Object[] aryObj = new Object[value.length];
        for (int i = 0; i < aryObj.length; ++i) {
            String v = "";
            if (value[i] != null) {
                v = value[i].toString();
            }
            aryObj[i] = getObjValue(type, v);
        }
        return aryObj;
    }
    
    private static Object getObjValue(final String type, final String paramValue) {
        Object value = null;
        if ("S".equals(type)) {
            value = paramValue;
        }
        else if ("SL".equals(type)) {
            value = "%" + paramValue + "%";
        }
        else if ("SLR".equals(type)) {
            value = paramValue + "%";
        }
        else if ("SLL".equals(type)) {
            value = "%" + paramValue;
        }
        else if ("SUPL".equals(type)) {
            value = "%" + paramValue.toUpperCase() + "%";
        }
        else if ("SUPLR".equals(type)) {
            value = paramValue.toUpperCase() + "%";
        }
        else if ("SUPLL".equals(type)) {
            value = "%" + paramValue.toUpperCase();
        }
        else if ("SLOL".equals(type)) {
            value = "%" + paramValue.toLowerCase() + "%";
        }
        else if ("SLOLR".equals(type)) {
            value = paramValue.toLowerCase() + "%";
        }
        else if ("SLOLL".equals(type)) {
            value = "%" + paramValue.toLowerCase();
        }
        else if ("L".equals(type)) {
            value = new Long(paramValue);
        }
        else if ("N".equals(type)) {
            value = new Integer(paramValue);
        }
        else if ("DB".equals(type)) {
            value = new Double(paramValue);
        }
        else if ("BD".equals(type)) {
            value = new BigDecimal(paramValue);
        }
        else if ("FT".equals(type)) {
            value = new Float(paramValue);
        }
        else if ("SN".equals(type)) {
            value = new Short(paramValue);
        }
        else if ("UDL".equals(type)) {
            value = paramValue;
        }
        else if ("UDG".equals(type)) {
            value = paramValue;
        }
        else if ("SDL".equals(type)) {
            value = paramValue.substring(0, 4) + paramValue.substring(5, 7) + paramValue.substring(8, 10);
        }
        else if ("SDG".equals(type)) {
            value = paramValue.substring(0, 4) + paramValue.substring(5, 7) + paramValue.substring(8, 10);
        }
        else {
            value = paramValue;
        }
        return value;
    }
    
    public static int getInt(final String key) {
        return getInt(key, 0);
    }
    
    public static int getInt(final String key, final int defaultValue) {
        final String str = RequestHelper.REQUEST.get().getParameter(key);
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        }
        return Integer.parseInt(str);
    }
    
    public static String getPrePage() {
        if (StringUtils.isEmpty(RequestHelper.REQUEST.get().getParameter("returnUrl"))) {
            return RequestHelper.REQUEST.get().getHeader("Referer");
        }
        return RequestHelper.REQUEST.get().getParameter("returnUrl");
    }
    
    public static Long[] getLongAryByStr(final String key) {
        final String str = RequestHelper.REQUEST.get().getParameter(key);
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        final String[] aryId = str.split(",");
        final Long[] lAryId = new Long[aryId.length];
        for (int i = 0; i < aryId.length; ++i) {
            lAryId[i] = Long.parseLong(aryId[i]);
        }
        return lAryId;
    }
    
    public static long getLong(final String key) {
        return getLong(key, 0L);
    }
    
    public static long getLong(final String key, final long defaultValue) {
        final String str = RequestHelper.REQUEST.get().getParameter(key);
        if (StringUtils.isEmpty(str)) {
            return defaultValue;
        }
        return Long.parseLong(str.trim());
    }
    
    static {
        LOGGER = LoggerFactory.getLogger((Class)RequestHelper.class);
        REQUEST = new ThreadLocal<HttpServletRequest>();
    }
}
